<?php

namespace Xrfg\Api\Trading\Spot\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Xrfg\Api\Trading\Spot\Resources\SymbolV2Resource;
use Xrfg\Api\Trading\Spot\Resources\TickerResource;
use Xrfg\Core\Trading\Account\Models\Coin;
use Xrfg\Core\Trading\Account\Models\CoinIntroduce;
use Xrfg\Core\Trading\Spot\Models\Market;
use Xrfg\Core\Trading\Spot\Models\Pair;
use Xrfg\Api\Trading\Spot\Requests\CoinIntroduceRequest;
use Xrfg\Api\Trading\Spot\Resources\CoinIntroduceResource;
use Xrfg\Api\Trading\Spot\Resources\CoinResource;
use Xrfg\Api\Trading\Spot\Resources\MarketResource;
use Xrfg\Api\Trading\Spot\Resources\SymbolResource;

class CommonController extends Controller
{
    /**
     * 交易区.
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function markets()
    {
        $markets = Cache::remember('table:trading_spot_markets', now()->addDay(), function () {
            return Market::select(['id', 'name'])->where('status', 1)->get();
        });

        return MarketResource::collection($markets);
    }

    /**
     * 交易对.
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function symbols()
    {
        $symbol = request('symbol');
        /** @var Collection $pairs */
        $pairs = Cache::remember('table:trading_spot_pairs', now()->addMinute(), function () {
            return Pair::all();
        });

        $pairs = $pairs->whereIn('status', [Pair::STATUS_PRE_TRADING, Pair::STATUS_TRADING, Pair::STATUS_BREAK])
            ->when($symbol, function ($query) use ($symbol) {
                /* @var Builder $query */
                return $query->where('symbol', $symbol);
            });

        return SymbolResource::collection($pairs);
    }

    /**
     * v2交易对.
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function symbolV2()
    {
        $symbol = request('symbol');
        $pairs = Pair::whereIn('status', [Pair::STATUS_PRE_TRADING, Pair::STATUS_TRADING, Pair::STATUS_BREAK])
            ->when($symbol, function ($query) use ($symbol) {
                /* @var Builder $query */
                return $query->where('symbol', 'like', "%{$symbol}%");
            })->get();

        return SymbolV2Resource::collection($pairs);
    }

    /**
     * 行情列表.
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function tickers()
    {
        $symbol = request('symbol');
        $order_name = in_array(request('order_name'), ['asc', 'desc']) ? request('order_name') : '';
        $order_price = request('order_price');
        $order_rate = request('order_rate');

        if ($order_name || $order_price || $order_rate) { // 需要排序
            $pairs = Pair::when($order_name, function ($query) use ($order_name) {
                return $query->orderBy('symbol', $order_name);
            })->get();
            // TODO : 价格排序
            // TODO : 涨跌幅排序
        } else {
            /** @var Collection $pairs */
            $pairs = Cache::remember('table:trading_spot_pairs', now()->addMinute(), function () {
                return Pair::all();
            });
        }

        $pairs = $pairs->whereIn('status', [Pair::STATUS_PRE_TRADING, Pair::STATUS_TRADING, Pair::STATUS_BREAK])
            ->when($symbol, function ($query) use ($symbol) {
                /* @var Builder $query */
                return $query->where('symbol', $symbol);
            });

        return TickerResource::collection($pairs);
    }

    /**
     * 币种.
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function currencies(Request $request)
    {
        $coinName = $request->input('coin_name');
        $coins = Coin::when($coinName, function ($query) use ($coinName) {
            $query->where('name', 'like', "%{$coinName}%");
        })->get();

        return CoinResource::collection($coins);
    }

    /**
     * 币种简介.
     *
     * @param CoinIntroduceRequest $request
     *
     * @return CoinIntroduceResource
     */
    public function introduce(CoinIntroduceRequest $request)
    {
        $coin_name = $request->input('coin_name');
        $introduce = CoinIntroduce::where('coin_id', Coin::where('name', $coin_name)->value('id'))->first();

        return new CoinIntroduceResource($introduce);
    }

    /**
     * 热门搜索币种.
     * @return \Illuminate\Http\Response
     */
    public function hotCoin()
    {
        return $this->array([
            'BTC',
            'USDT',
            'ETH'
        ]);
    }
}
